package com.dw.dwdzcommerce.view.mainContent.ad;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.base.activity.BaseActivity;
import com.dw.dwdzcommerce.entity.AdPage;
import com.dw.dwdzcommerce.tools.commons.L;
import com.dw.dwdzcommerce.tools.commons.viewUtil.ViewUtil;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ADPagerLayout2 implements OnPageChangeListener {
	private static final String TAG = "ADPagerLayout";
	private static final long DELAY_MILLIS = 4000;
	private Fragment mFragment;
	private RelativeLayout mRoot;
	private static ViewPager mVp;
	private ADCoinStageLayout mStageLayout;
	private PagerChangeHandler mChangePagerHandler;
	private Timer timer;
	private FragmentActivity mContext;

	public void initModule(Fragment fragment) {
		mChangePagerHandler = new PagerChangeHandler();
		mFragment = fragment;
		mRoot = (RelativeLayout) LayoutInflater.from(mFragment.getActivity()).inflate(R.layout.item_adpager_viewpager,
				null);
		mVp = (ViewPager) mRoot.findViewById(R.id.item_adpager_vp);

		mVp.addOnPageChangeListener(this);
		mStageLayout = new ADCoinStageLayout();

		mStageLayout.initModule((BaseActivity) mFragment.getActivity(), mRoot);
		// mStageLayout.initSize(6);
	}

	public void initModule(FragmentActivity context) {
		mChangePagerHandler = new PagerChangeHandler();
		mContext = context;
		mRoot = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.item_adpager_viewpager, null);
		mVp = (ViewPager) mRoot.findViewById(R.id.item_adpager_vp);

		mVp.addOnPageChangeListener(this);
		mStageLayout = new ADCoinStageLayout();

		mStageLayout.initModule((BaseActivity) mContext, mRoot);
	}

	public ViewGroup getADView() {
		return mRoot;
	}

	public void initData(List<AdPage> pages) {
		if (pages.isEmpty()) {
			Log.e(TAG, "ADPage pages is empty");
			return;
		}
		int size = pages.size();
		mStageLayout.initSize(size);
		ADPagerAdapter adapter = null;
		if (mFragment != null) {

			adapter = new ADPagerAdapter(mFragment.getFragmentManager(), pages);
		} else {
			adapter = new ADPagerAdapter(mContext.getSupportFragmentManager(), pages);
		}
		mVp.setAdapter(adapter);

		ruleViewPager();
	}

	private void ruleViewPager() {
		mVp.post(new Runnable() {

			@Override
			public void run() {
				LayoutParams layoutParams = mVp.getLayoutParams();
				int width = layoutParams.width;
				if (width == LayoutParams.MATCH_PARENT || width == LayoutParams.WRAP_CONTENT) {
					int screenWidth = 0;
					if (mFragment != null) {
						screenWidth = ViewUtil.getScreenWidth(mFragment.getActivity().getApplicationContext());

					} else {
						screenWidth = ViewUtil.getScreenWidth(mContext.getApplicationContext());
					}
					width = screenWidth;
				}

				int height = (int) (width / 1.6);

				layoutParams.width = width;
				layoutParams.height = height;
				mVp.setLayoutParams(layoutParams);
				mVp.setCurrentItem(0);
				startPageChange(0);
			}
		});
	

	}

	class ADPagerAdapter extends FragmentPagerAdapter {

		private List<Fragment> mFragments;

		public ADPagerAdapter(FragmentManager fm, List<AdPage> datas) {
			super(fm);
			mFragments = ADItemFragment.mkFragments(datas);
		}

		@Override
		public Fragment getItem(int index) {
			return mFragments.get(index);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		L.i(TAG, "onPageScrollStateChanged");
		switch (arg0) {
		case ViewPager.SCROLL_STATE_DRAGGING:
			if (timer != null) {
				timer.cancel();
				timer = null;
			}
			break;
		case ViewPager.SCROLL_STATE_IDLE:
			int currentItem = mVp.getCurrentItem();
			startPageChange(currentItem);
			break;
		default:
			break;
		}

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int index) {
		L.i(TAG, "index=" + index);
		// startPageChange(index);
	}

	private void startPageChange(final int index) {
		mStageLayout.select(index);

		timer = new Timer(true);
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				int tempINdex = index;
				mChangePagerHandler
						.sendMessage(mChangePagerHandler.obtainMessage(100, tempINdex, mStageLayout.getItemSize()));
			}
		}, DELAY_MILLIS);

	}

	static class PagerChangeHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			int currIndex = msg.arg1;
			int itemSize = msg.arg2;
			int nextIndex = ++currIndex % itemSize;
			mVp.setCurrentItem(nextIndex, true);

		}
	}

}
